<template>
  <div>
    <div class="flex items-center justify-between">
      <div class="title">汇总表</div>
      <el-button :disabled="selectionList.length === 0" type="primary" size="small" @click="handlePrintAll">
        批量打印
      </el-button>
    </div>
    <ClTable
      v-if="loaded"
      class="table-report"
      show-index
      show-selection
      :table-data="tableData"
      show-border
      stripe
      :column-list="columnList"
      :show-page="false"
      @handle-selection-change="handleSelectionChange"
    >
      <template #column>
        <el-table-column label="操作" width="200">
          <template #default="scope">
            <el-button v-if="scope.row._VIEW || scope.row.id" type="primary" link plain @click="handleView(scope.row)">
              查看
            </el-button>
            <el-button type="primary" link plain @click="handlePrint(scope.row)">打印</el-button>
          </template>
        </el-table-column>
      </template>
    </ClTable>
  </div>
  <ViewDialog v-if="hookVisible" v-model="hookVisible" :info="dialogInfo" :type="type" @at-close="onCloseDialog" />
  <SummaryViewDialog
    v-if="hookVisible2"
    v-model="hookVisible2"
    :info="dialogInfo2"
    :type="type2"
    @at-close="onCloseDialog2"
  />
</template>

<script setup lang="ts">
import { ElMessage } from 'element-plus'
import ViewDialog from './components/ViewDialog.vue'
import SummaryViewDialog from './components/SummaryViewDialog.vue'
import { previewPDFInNewWindow } from '@/utils/pdf-utils'
import {
  commonExport,
  exportItemSummary,
  exportScoringDetailsSummary,
  getScoringDetailsSummary
} from '@/api/scoring-details-summary'
onMounted(() => {
  getTableData()
})
const loaded = ref(false)
const tableData = ref<any[]>([])
const itemIds = ref<any[]>([])

// 评标报告 评审报告 区分
const BID_TYPE_MAP = ['邀请招标', '公开招标（500万元以上）', '公开招标（500万元以下）']
const defaultStart1 = [{ name: '评标报告', _LOCAL: true, _VIEW: false }]
const defaultStart2 = [{ name: '评审报告', _LOCAL: true, _VIEW: false }]
const defaultEnd = [{ name: '评标汇总', _LOCAL: true, _VIEW: true }]
const getTableData = async () => {
  try {
    loaded.value = false
    const res: any = await getScoringDetailsSummary()
    itemIds.value = res.map((item: any) => item.itemId)
    // 判断是否是评审报告 CG 是评标报告 GC 是评审报告
    const projectInfo = useProjectInfoStore().projectInfo
    const { bidType } = projectInfo

    let defaultStart: any = []
    if (BID_TYPE_MAP.includes(bidType)) {
      defaultStart = defaultStart1
    } else {
      defaultStart = defaultStart2
    }

    if (res.length > 0) {
      tableData.value = [...defaultStart, ...res, ...defaultEnd]
    } else {
      tableData.value = [...defaultStart, ...defaultEnd]
    }
    loaded.value = true
  } catch (error) {
    console.log('🐷 🐽 getTableData 🐽 error👉👉👉', error)
    loaded.value = true
  }
}
const selectionList = ref<any[]>([])
const handleSelectionChange = (val: any) => {
  selectionList.value = val
}
const columnList = ref([
  {
    label: '报告名称',
    prop: 'name'
  }
])

const handlePrintAll = async () => {
  try {
    const projectId = useProjectInfoStore().projectInfo.id
    const templateList = selectionList.value.map((item: any) => item.name)
    const itemIdList = selectionList.value.filter((item: any) => !item._LOCAL).map((item: any) => item.id)
    const { fileName, fileStream }: any = await exportScoringDetailsSummary({ projectId, templateList, itemIdList })
    previewPDFInNewWindow(fileStream, fileName)
  } catch (error) {
    console.log('🐷 🐽 handlePrintAll 🐽 error👉👉👉', error)
  }
}
const { type, hookVisible, dialogInfo, onOpenDialog, onCloseDialog } = useDialog(null)
const {
  type: type2,
  hookVisible: hookVisible2,
  dialogInfo: dialogInfo2,
  onOpenDialog: onOpenDialog2,
  onCloseDialog: onCloseDialog2
} = useDialog(null)
const handleView = (row: any) => {
  if (row.name === '评标汇总') {
    type2.value = '评标汇总'
    onOpenDialog2({ ...row, itemIds: itemIds.value })
    return
  }
  if (row.id) {
    type.value = '评标报告查看'
    onOpenDialog(row)
    return
  }
}

const API_MAP = {
  评审报告: 'exportBidReport',
  评标报告: 'exportBidReport',
  开标记录表: 'exportBidRecord',
  招标评标委员会签到单: 'exportJudgeSign',
  招标工作人员签到单: 'exportStaffSign',
  招标开标会供应商签到单: 'exportSupplierSign',
  评委承诺书: 'exportCommitment',
  暗标对照表: 'exportDarkContrast',
  评标汇总: 'exportBidResultSummary'
}
const handlePrint = async (row: any) => {
  // 接口评审项
  if (!row._LOCAL) {
    const projectId = useProjectInfoStore().projectInfo.id
    const { fileName, fileStream }: any = await exportItemSummary({ projectId, itemId: row.itemId })
    previewPDFInNewWindow(fileStream, fileName)
    return
  }
  if (API_MAP[row.name]) {
    const { fileName, fileStream }: any = await commonExport(API_MAP[row.name])
    previewPDFInNewWindow(fileStream, fileName)
    return
  }
  ElMessage.warning('暂无模板')
}
</script>

<style lang="scss" scoped>
.title {
  font-weight: 600;
  font-size: 20px;
  color: #242222;
}
</style>
